package com.ptl.bp.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ptl.bp.entity.UserEntity;
import com.ptl.bp.mapper.UserMapper;
import com.ptl.bp.util.BoolUtils;
import org.springframework.stereotype.Service;

@Service
public class UserService extends ServiceImpl<UserMapper, UserEntity> {
    public boolean createUser(UserEntity user) {
        if (isExists(user)) {
            throw new RuntimeException("用户名已存在");
        }
        return save(user);
    }

    public boolean isExists(UserEntity user) {
        return this.lambdaQuery()
                .eq(UserEntity::getUsername, user.getUsername())
                .ne(BoolUtils.toBool(user.getId()), UserEntity::getId, user.getId())
                .exists();
    }
}